<?php
/**
 * author Lty
 * desc curd增删改查 testUrl:127.0.0.1/api_plus.php/banner/list
 * banner(表名) list(操作)
 * list/查询 add/增加 update/修改 del/删除
 */
header("content-type:text/html;charset=utf-8");
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
class curd{
    private $url;
    private $tableName;
    private $action;
    private $connect;
    public function __construct(){
        $host = 'localhost';
        $username = 'root';
        $password = '';
        $db = 'test';
        // 创建连接
        $this->connect = mysqli_connect($host, $username, $password,$db);
        mysqli_query($this->connect,'set names utf8');
    }
    public function init(){
        $number = $_SERVER['PHP_SELF'];
        $result = substr($number,strripos($number,'.php')+5);
        $con = explode('/',$result);
        $this->url = $result;
        $this->tableName = $con[0];
        $this->action = $con[1];
        switch($this->action){
            case 'list':
                $this->lists();break;
            case 'del':
                $this->del();break;
            case 'add':
                $this->add();break;
            case 'update':
                $this->update();break;
            default:
                $this->error();
        }
    }
    public function error(){
        echo '非法操作！';
    }
    public function lists(){
        $pageCurrent = isset($_GET['pageCurrent'])?$_GET['pageCurrent']:0;
        $pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $search = isset($_GET['search'])?$_GET['search']:'';
        if($pageCurrent == 1 || $pageCurrent == 0){
            $pageOffset = 0;
        }else{
            $pageOffset =($pageCurrent-1)*$pageSize;
        }

        $dataSql = "select * from $this->tableName";
        if($search){
            $dataSql .= " where name like '%$search%'";
        }
        $dataSql .=" order by id desc limit $pageOffset,$pageSize";
        $data = $this->getdata($dataSql);
        $countSql = "select count(*) as count from $this->tableName";
        if($search){
            $countSql .= " where name like '%$search%'";
        }
        $result = $this->getdata($countSql);
        $count = $result[0]['count'];
        $res['data'] = $data;
        $res['count'] = $count;
        $this->result(200,'success',$res);
    }

    public function del(){
        if(!isset($_POST['id'])){
            return $this->result(400,'id必传！',[]);
        }
        $sql = "DELETE FROM $this->tableName WHERE id in ($_POST[id])";
        $result = mysqli_query($this->connect, $sql);
        if($result){
            $this->result(200,'success',[]);
        }else{
            $this->result(500,'error',[]);
        }
    }
    public function add(){
        if(count($_POST)){
            $names = join(',',array_keys($_POST));
            $valueArr = array_values($_POST);
            $values = '';
            for($i=0;$i<count($valueArr);$i++){
                $values .="'$valueArr[$i]',";
            }
            $values = substr($values,0,strlen($values)-1);
            $sql = "insert into $this->tableName ($names) values ($values)";
        }
        $result = mysqli_query($this->connect, $sql);
        if($result){
            $this->result(200,'success',[]);
        }else{
            $this->result(500,'error',[]);
        }
    }
    public function update(){
        if(!isset($_POST['id'])){
            return $this->result(400,'id必传！',[]);
        }
        if(count($_POST)){
            $set = '';
            $names = array_keys($_POST);
            for($i=0;$i<count($names);$i++){
                $set .= " $names[$i] = '".$_POST[$names[$i]]."' ,";
            }
            $set = substr($set,0,strlen($set)-1);
            $sql = "UPDATE $this->tableName SET $set WHERE id = $_POST[id]";
            $result = mysqli_query($this->connect, $sql);
            if($result){
                $this->result(200,'success',[]);
            }else{
                $this->result(500,'error',[]);
            }
        }else{
            $this->result(400,'请先传入参数',[]);
        }

    }
    public function getdata($sql){
        $result = mysqli_query($this->connect, $sql);
        $arr = [];
        if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
                $arr[] = $row;
            }
        }
        return $arr;
    }

    public function result($code,$msg,$data){
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
        echo json_encode($result);
    }
}
$curd = new curd();
$curd->init();